package com.project.rfidpage.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.project.rfidpage.dto.RidMaintenanceDto;
import com.project.rfidpage.dto.RidTidMaintenanceParam;
import com.project.rfidpage.dto.RidTidMaintenanceRecordDto;
import com.project.rfidpage.entity.RidMaintenance;
import com.project.rfidpage.entity.RidMaintenanceRecord;
import com.project.rfidpage.mapper.RidMaintenanceMapper;
import com.project.rfidpage.mapper.RidMaintenanceRecordMapper;
import com.project.rfidpage.service.RidMaintenanceService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

@Service
public class RidMaintenanceServiceImpl implements RidMaintenanceService {

    @Resource
    private RidMaintenanceMapper ridMaintenanceMapper;

    @Resource
    private RidMaintenanceRecordMapper ridMaintenanceRecordMapper;

    @Override
    @DS("forth")
    public List<RidMaintenanceDto> getListForPage(RidTidMaintenanceParam param) {
        return ridMaintenanceMapper.selectListByUid(param);
    }

    @Override
    @DS("forth")
    public List<RidTidMaintenanceRecordDto> getRecordListForPage(RidTidMaintenanceParam param) {
        return ridMaintenanceRecordMapper.selectListByUid(param);
    }

    @Override
    @DS("forth")
    public List<RidMaintenance> getById(Long id) {
        Example example = new Example(RidMaintenance.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("task_id", id);
        return ridMaintenanceMapper.selectByExample(example);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("forth")
    public void insert(RidMaintenance entity) {
        ridMaintenanceMapper.insertSelective(entity);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("forth")
    public void update(RidMaintenance entity, Long idBefore) {
        Example example = new Example(RidMaintenance.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("task_id", idBefore);
        ridMaintenanceMapper.updateByExampleSelective(entity, example);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    @DS("forth")
    public void insertRecord(RidMaintenanceRecord entity) {
        ridMaintenanceRecordMapper.insertSelective(entity);
    }
}
